package com.lightman.gulimall.ware.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lightman.common.utils.PageUtils;
import com.lightman.common.utils.Query;
import com.lightman.gulimall.ware.dao.PurchaseDetailDao;
import com.lightman.gulimall.ware.entity.PurchaseDetailEntity;
import com.lightman.gulimall.ware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        /**
         *  status: 0 //状态
         *  wareId:1 仓库
         *  key=&status=&wareId=1
         */

        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();

        // 检索
        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                wrapper.eq("purchase_id", key)
                        .or().eq("sku_id", key);
            });
        }

        // 状态
        String status = (String)params.get("status");
        if(!StringUtils.isEmpty(status)){
           queryWrapper.eq("status", status);
        }

        // 仓库
        String wareId = (String)params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {

        List<PurchaseDetailEntity> purchaseId = this.list(new QueryWrapper<PurchaseDetailEntity>()
                .eq("purchase_id", id));

        return purchaseId;
    }

}